home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5885 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  56 lines

  1. Path: ix.netcom.com!netnews
  2. From: kakima@ix.netcom.com (Kiyoshi Akima)
  3. Newsgroups: comp.lang.c++
  4. Subject: GCC/STL problem
  5. Date: 7 Feb 1996 05:05:36 GMT
  6. Organization: Netcom
  7. Message-ID: <4f9bv0$iec@cloner3.netcom.com>
  8. NNTP-Posting-Host: ix-den12-15.ix.netcom.com
  9. X-NETCOM-Date: Tue Feb 06  9:05:40 PM PST 1996
  10.  
  11. I'm having a problem with a destructor getting called twice.
  12.  
  13. //begin code
  14. #include <iostream.h>
  15. #include <list.h>
  16.  
  17. class Foo {
  18. public:
  19.     Foo(const int i = 0): i_(i) {}
  20.     Foo(const Foo& f): i_(f.i_) {}
  21.     Foo& operator =(const Foo& f) { i_ = f.i_; }
  22.     ~Foo() {
  23.         cout << "dtor(" << i_ << "): " << (void*)this << '\n';
  24.         i_ = -1;
  25.     }
  26. private:
  27.     int i_;
  28. };
  29.  
  30. int main()
  31. {
  32.     list<Foo> f;
  33.     f.push_back(Foo(1));
  34.     return 0;
  35. }
  36. // end code
  37.  
  38. The code doesn't do much.  When the destructor gets called, it displays
  39. its address and sets the integer member i_ to -1.
  40. When I compile and run with g++ 2.7.2 on Linux using the bundled STL I
  41. get the following output:
  42.  
  43. dtor(1): 0xbffffbc8
  44. dtor(1): 0x8009b78
  45. dtor(-1): 0x8009b78
  46. dtor(0): 0x8009dd8
  47.  
  48. I understand the temporary object getting constructed and destructed.
  49. I also understand the "empty" object getting placed in the list.
  50. But the third line of output tells me that the element in the list is
  51. getting destructed TWICE.
  52.  
  53. Kiyoshi Akima
  54. kakima@ix.netcom.com
  55.  
  56.